package com.gitee.chessboard.jdbc.parser;

import com.gitee.chessboard.jdbc.JdbcManager;

/**
 * 将对象转换成delete的sql
 *
 * @author 敲代码的旺财
 * @date 2021/11/25 13:37
 */
public class DeleteParser implements Parser {
    /**
     * jdbc的管理器
     */
    private JdbcManager jdbcManager;

    public DeleteParser(JdbcManager jdbcManager) {
        this.jdbcManager = jdbcManager;
    }

    /**
     * 将对象转成删除数据的sql
     *
     * @param obj 需要解析的对象
     * @return 删除sql语句
     */
    @Override
    public String convert(Object obj) {
        ParserHandler parserHandler = jdbcManager.getParserHandler(obj);
        String tableId = parserHandler.getTableId();
        StringBuilder sb = new StringBuilder("DELETE FROM ");
        sb.append(parserHandler.getTableName())
                .append(" WHERE ")
                .append(tableId)
                .append(" = #{")
                .append(parserHandler.getTableFieldNameAndFieldName().get(tableId))
                .append("}");
        return sb.toString();
    }
}
